package com.ja.openchat.views.bubble;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ja.openchat.R;
import com.ja.openchat.ui.chat.adapter.ConversationMessageAdapter;
import com.lxj.xpopup.core.AttachPopupView;
import com.zy.openchat.Constants;
import com.zy.openchat.core.model.setting.MessageAttachMenuEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import kotlin.Triple;

public class MessageHandlePopup extends AttachPopupView {
    private RecyclerView recyclerView;
    private OnMessageAttachListener mOnMessageAttachListener;
    private ConversationMessageAdapter.ContextMenuItemWrapper copyUrlMenuItem;
    private List<Triple<String,Integer, ConversationMessageAdapter.ContextMenuItemWrapper>> data;
    public MessageHandlePopup(@NonNull Context context, List<Triple<String,Integer, ConversationMessageAdapter.ContextMenuItemWrapper>> data
            , ConversationMessageAdapter.ContextMenuItemWrapper copyUrlMenu, OnMessageAttachListener listener) {
        super(context);
        mOnMessageAttachListener = listener;
        this.data = data;
        this.copyUrlMenuItem = copyUrlMenu;
    }
    private RecyclerView emojiRecyclerView;
    @Override
    protected void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        emojiRecyclerView = findViewById(R.id.emojiRecyclerView);
        emojiRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        OnMessageAttachAdapter mAdapter = new OnMessageAttachAdapter(data);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if(mOnMessageAttachListener!=null){
                    mOnMessageAttachListener.onClickItem(mAdapter.getItem(position).getThird());
                }
                dismiss();
            }
        });
        recyclerView.setAdapter(mAdapter);
        MessageHandleEmojiAdapter emojiAdapter = new MessageHandleEmojiAdapter(Constants.messageEmojiIndexs);
        emojiRecyclerView.setAdapter(emojiAdapter);
        emojiAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if(mOnMessageAttachListener!=null){
                    mOnMessageAttachListener.onReactionClick(position,emojiAdapter.getItem(position));
                }
                dismiss();
            }
        });
    }
    private boolean isAddCopyUrl = false;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageMenuEvent(MessageAttachMenuEvent event) {
        if(copyUrlMenuItem==null||isAddCopyUrl){
            return;
        }
        isAddCopyUrl = true;
        data.add(new Triple<>("复制链接",R.mipmap.menu_copy,copyUrlMenuItem));
        if(recyclerView!=null&&recyclerView.getAdapter()!=null){
            recyclerView.getAdapter().notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_attach_popu;
    }
}
